package middleware

import (
	"a-common/auth/permsauth"
	"strings"

	"a-common/constant"
	"a-common/domain/ctx"
	"a-common/myerr"

	"github.com/gofiber/fiber/v2"
)

// PermsHandler 权限校验
func PermsHandler(perms string) fiber.Handler {
	return func(c *fiber.Ctx) error {
		user := c.UserContext().Value(ctx.UserCtxKey).(ctx.UserCtx)
		// 超级管理员直接放行
		if user.Uid == constant.ADMIN_ID {
			return c.Next()
		}
		rs := strings.Split(user.Role, ",")
		auth := permsauth.AuthPerms(&rs, perms)
		if auth {
			return c.Next()
		}
		return c.Status(myerr.CODE_403).JSON(myerr.ERR_SYS_403)
	}
}
